{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 第5章.序列转化模型\n",
    "<p>我们将学习如何将一种序列转化为另一种序列,比如将英语翻译成法语。又或者，在语音识别领域中,输入的是语音，输出的却是文字</p>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5.1.循环神经网络模型(RNN)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "RNNCell(5, 7)"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from torch import nn\n",
    "rnn_cell = nn.RNNCell(input_size=5,hidden_size=7) # 输入向量的特征维度,隐藏向量的特征维度\n",
    "rnn_cell"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:34:57.623635300Z",
     "start_time": "2023-10-18T11:34:31.398171300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "Parameter containing:\ntensor([[-0.0497,  0.1135,  0.1744, -0.1810,  0.2242],\n        [ 0.3161, -0.0487, -0.2522, -0.0173,  0.3053],\n        [-0.0050,  0.0024, -0.0014, -0.2383,  0.1104],\n        [ 0.2224, -0.2283, -0.0364, -0.2700, -0.0625],\n        [-0.1581, -0.3335, -0.1431,  0.0790,  0.1449],\n        [-0.0252,  0.1815, -0.1240, -0.1062,  0.2790],\n        [ 0.1073, -0.0706, -0.1228,  0.1525,  0.2883]], requires_grad=True)"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rnn_cell.weight_ih"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:42:15.780485100Z",
     "start_time": "2023-10-18T11:42:13.935160Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[-0.9737,  0.2168,  0.6594, -0.6795,  0.0277, -0.5690, -0.5193]],\n       grad_fn=<TanhBackward0>)"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "input = torch.randn(1,5)\n",
    "hidden = torch.randn(1,7)\n",
    "rnn_cell(input,hidden)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:44:02.061450500Z",
     "start_time": "2023-10-18T11:44:01.283527Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[[-0.7026, -0.9378,  0.5252,  0.4582, -0.6077,  0.7981,  0.5265],\n          [-0.0683, -0.7831, -0.2321, -0.2192,  0.4944, -0.3496, -0.6281]],\n \n         [[-0.7742, -0.5964, -0.0319,  0.3442,  0.4213,  0.4673, -0.0704],\n          [ 0.5441, -0.7591, -0.6637, -0.2813, -0.3474, -0.1589,  0.2613]],\n \n         [[-0.2762, -0.2495,  0.2334, -0.0095, -0.8631, -0.5371, -0.2116],\n          [-0.4858, -0.3106, -0.7004,  0.2011,  0.2990, -0.3359, -0.7174]]],\n        grad_fn=<StackBackward0>),\n tensor([[[-0.2762, -0.2495,  0.2334, -0.0095, -0.8631, -0.5371, -0.2116],\n          [-0.4858, -0.3106, -0.7004,  0.2011,  0.2990, -0.3359, -0.7174]]],\n        grad_fn=<StackBackward0>))"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先初始化一个rnn\n",
    "rnn = nn.RNN(input_size=5,hidden_size=7)\n",
    "input = torch.randn(3,2,5) # (seq,batch,input_size) seq为序列长度\n",
    "hidden = torch.randn(1,2,7)\n",
    "rnn(input,hidden)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:45:25.228624700Z",
     "start_time": "2023-10-18T11:45:24.976831800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.LSTM模型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "LSTMCell(5, 7)"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lstm_cell = nn.LSTMCell(input_size=5,hidden_size=7)\n",
    "lstm_cell"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:48:30.418034400Z",
     "start_time": "2023-10-18T11:48:30.411037Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[-0.1598,  0.0807,  0.3286,  0.1341, -0.0398, -0.0700, -0.0806]],\n        grad_fn=<MulBackward0>),\n tensor([[-0.9386,  0.0959,  0.7081,  0.2344, -0.0754, -0.1296, -0.5080]],\n        grad_fn=<AddBackward0>))"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input = torch.randn(1,5)\n",
    "h0 = torch.randn(1,7)\n",
    "c0 = torch.randn(1,7)\n",
    "lstm_cell(input,(h0,c0))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:50:06.794666400Z",
     "start_time": "2023-10-18T11:50:06.255206300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "lstm = nn.LSTM(input_size=5,hidden_size=7)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:55:12.592957300Z",
     "start_time": "2023-10-18T11:55:12.583955900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "input = torch.randn(3,2,5)\n",
    "h0 = torch.randn(1,2,7)\n",
    "c0 = torch.randn(1,2,7)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:56:06.090874700Z",
     "start_time": "2023-10-18T11:56:06.074888200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([3, 2, 7])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output,(h1,c1) = lstm(input,(h0,c0))\n",
    "output.size()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:57:23.677790900Z",
     "start_time": "2023-10-18T11:57:22.741790200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([1, 2, 7])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h1.size()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:57:29.678308800Z",
     "start_time": "2023-10-18T11:57:29.663290900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([1, 2, 7])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1.size()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-18T11:57:45.253422800Z",
     "start_time": "2023-10-18T11:57:45.242432600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
